<?php



namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Area;
use app\common\model\Version;
use fast\Random;
use think\captcha\Captcha;
use think\Config;
use think\Db;
use think\Hook;

/**
 * 数据信息
 */
class Ranking extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    // 拜访排行
    public function lists1(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();


        $set=Db::name('work_set')->find();
        $md=$set['number'];
        $ds=$set['number1'];

        $sta=$this->request->param('stat');
        $endt=$this->request->param('endt');
        $times=strtotime($sta);
        $timesb=strtotime($endt);


        $sdate=date('ym',strtotime($sta));

        foreach ($quyu as &$key){

            $count=Db::name('client_log')
                ->where('quyu_id','eq',$key['id'])
                ->where('time','gt',$times)
                ->where('time','lt',$timesb)
                ->where('types','eq',1)
                ->count();

            $key['number']=$count;

            //本月请假
            $holiday=Db::name('holiday')
                ->where('date','eq',$sdate)
                ->where(['user_id'=>$key['user_id']])->count();

            $bili=0;
            if ($count>0){
                $bili=round( $count/($ds*($md-$holiday)),2);
            }

            $key['bili']=$bili;


        }





        // 调用冒泡排序函数
       $res=  bubbleSort($quyu);

        $this->success('本月拜访排行',$res);


    }



    // 新增客户
    public function lists2(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $sta=$this->request->param('stat');
        $endt=$this->request->param('endt');


        $times=strtotime($sta);
        $timesb=strtotime($endt);

        foreach ($quyu as &$key){

            $count=Db::name('aa_client')
                ->where('quyu_id','eq',$key['id'])
                ->where('time','gt',$times)
                ->where('time','lt',$timesb)
                ->count();

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);


    }



    // 工作时常
    public function lists3(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $sta=$this->request->param('stat');
        $endt=$this->request->param('endt');


        $times=strtotime($sta);
        $timesb=strtotime($endt);

        foreach ($quyu as &$key){

            $count=Db::name('workhour')
                ->where('quyu_id','eq',$key['id'])
                ->where('startime','gt',$times)
                ->where('startime','lt',$timesb)

                ->sum('hour');

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);


    }

    // 新增合作
    public function lists4(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $sta=$this->request->param('stat');
        $endt=$this->request->param('endt');


        $times=strtotime($sta);
        $timesb=strtotime($endt);

        foreach ($quyu as &$key){

            $count=Db::name('aa_client')
                ->where('quyu_id','eq',$key['id'])
                ->where('ctypestime','gt',$times)
                ->where('ctypestime','lt',$timesb)

                ->where('ctypes','eq',1)
                ->count();

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);


    }


    // 新增掉客
    public function lists5(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();


        $sta=$this->request->param('stat');
        $endt=$this->request->param('endt');


        $times=strtotime($sta);
        $timesb=strtotime($endt);


        foreach ($quyu as &$key){

            $count=Db::name('aa_client')
                ->where('quyu_id','eq',$key['id'])
                ->where('ctypestime','gt',$times)
                ->where('ctypestime','lt',$timesb)
                ->where('ctypes','eq',3)
                ->count();

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);


    }


    //获得一个本月时间
    public function getdate(){

        $a=date('Y-m').'-1';
        $b=date('Y-m-t', strtotime('today'));
        $this->success('本月时间',['a'=>$a,'b'=>$b]);

    }

    //获得一个本月时间
    public function gettodaydate(){

        $a=date('Y-m-d');
        $b=date('Y-m-t', strtotime('today'));
        $this->success('本日时间',['a'=>$a,'b'=>$b]);

    }

    //数据统计
    public function getalldatebytime(){

        $strtime=$this->request->param('statrtime');

        $endtime=$this->request->param('endtime');

        $strtimes=strtotime($strtime);
        $endtimes=strtotime($endtime)+86400;


        $use=$this->auth->getUser();

        $user=Db::name('user')->where(['id'=>$use['id']])->find();


        $data['data1']=Db::name('client_log')
                ->where('quyu_id','eq',$user['quyu_id'])
                ->where('time','gt',$strtimes)
                ->where('time','lt',$endtimes)
                ->count();

        //期间走访
        $see=Db::name('client_log')
            ->where('quyu_id','eq',$user['quyu_id'])
            ->where('time','gt',$strtimes)
            ->where('time','lt',$endtimes)
            ->group('aa_client_id')
            ->count();
        //我的全部
        $all=Db::name('aa_client')
            ->where('quyu_id','eq',$user['quyu_id'])
            ->count();

        $data['data2']=$all-$see;

        $data['data3']=Db::name('aa_client')
            ->where('quyu_id','eq',$user['quyu_id'])
            ->where('time','gt',$strtimes)
            ->where('time','lt',$endtimes)
            ->count();

        $data['data4']=Db::name('workhour')
            ->where('quyu_id','eq',$user['quyu_id'])
            ->where('startime','gt',$strtimes)
            ->where('endtime','lt',$endtimes)
            ->sum('hour');


        $data['data5']=Db::name('aa_client')
            ->where('quyu_id','eq',$user['quyu_id'])
            ->where('ctypestime','gt',$strtimes)
            ->where('ctypestime','lt',$endtimes)
            ->where('ctypes','eq',1)
            ->count();

        $data['data6']=Db::name('aa_client')
            ->where('quyu_id','eq',$user['quyu_id'])
            ->where('ctypestime','gt',$strtimes)
            ->where('ctypestime','lt',$endtimes)
            ->where('ctypes','eq',2)
            ->count();

        $data['data7']=Db::name('aa_client')
            ->where('quyu_id','eq',$user['quyu_id'])
            ->where('ctypestime','gt',$strtimes)
            ->where('ctypestime','lt',$endtimes)
            ->where('ctypes','eq',3)
            ->count();

        $this->success('数据统计',$data);


    }



    // 新增目标
    public function lists6(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $sta=$this->request->param('stat');
        $endt=$this->request->param('endt');

        $times=strtotime($sta);
        $timeend=strtotime($endt);

        foreach ($quyu as &$key){

            $count=Db::name('aa_client')
                ->where('quyu_id','eq',$key['id'])
                ->where('time','gt',$times)
                ->where('time','lt',$timeend)
                ->where('types','eq',1)
                ->count();

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);



    }


    // 拜访排行
    public function lists7(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();




        $sta=$this->request->param('stat');
        $endt=$this->request->param('endt');
        $times=strtotime($sta);
        $timesb=strtotime($endt);

        foreach ($quyu as &$key){

            $count=Db::name('client_log')
                ->where('quyu_id','eq',$key['id'])
                ->where('time','gt',$times)
                ->where('time','lt',$timesb)
                ->where('title','eq','膜拜')
                ->count();

            $key['number']=$count;




        }





        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月拜访排行',$res);


    }




}